package com.example.timerecorder;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;

public class SelectTimeActivity extends AppCompatActivity {

    public static final int RESULT_CODE_ADD_TIME = 2;
    public static final int RESULT_CODE_ALTER_TIME = 8;
    private static final String TAG = "selectTimeActivity";
    DatePicker selectDate;
    TimePicker selectTime;
    Button submitTimeBtn;

    int year, month, day, hour, minute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_time);

        //返回
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        selectDate = findViewById(R.id.selectDate);
        selectTime = findViewById(R.id.selectTime);
        submitTimeBtn = findViewById(R.id.submitTimeBtn);

        //使用intent接收当前选择日期时间
        Intent intent = getIntent();
        String currentDate = intent.getStringExtra("current_date");
        String currentTime = intent.getStringExtra("current_time");
        String flag = intent.getStringExtra("flag");
        String[] date = currentDate.split("-");
        String[] time = currentTime.split(":");

        year = Integer.valueOf(date[0]);
        month = Integer.valueOf(date[1]);
        day = Integer.valueOf(date[2]);
        hour = Integer.valueOf(time[0]);
        minute = Integer.valueOf(time[1]);

        //对datepicker初始化
        selectDate.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
        selectDate.init(year, month-1, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
            }
        });

        //对timePicker初始化
        selectTime.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);  //设置点击事件不弹键盘
        selectTime.setIs24HourView(true);   //设置时间显示为24小时
        selectTime.setHour(hour);  //设置当前小时
        selectTime.setMinute(minute); //设置当前分（0-59）

        //点击保存，
        submitTimeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取当前选择日期
                year = selectDate.getYear();
                month = selectDate.getMonth()+1;
                day = selectDate.getDayOfMonth();
                //获取当前选择时间
                hour = selectTime.getHour();
                minute = selectTime.getMinute();
                Log.i(TAG, "onClick: hour:"+hour);
                Log.i(TAG, "onClick: min:"+minute);

                String sdate = year + "-" + formatDateDouble(month) + "-" + formatDateDouble(day);
                String stime = formatDateDouble(hour) + ":" + formatDateDouble(minute);

                if(flag.equals("add")){
                    //intent——将time从activity传到mainActivity的fragment中
                    Intent submitTime = new Intent(SelectTimeActivity.this, MainActivity.class);
                    submitTime.putExtra("select_date", sdate);
                    submitTime.putExtra("select_time", stime);
                    Log.i(TAG, "onClick: select_time:" + stime);
                    setResult(RESULT_CODE_ADD_TIME, submitTime);
                }
                if(flag.equals("alter")){
                    //intent——将time从activity传到alterRecordActivity中
                    Intent alterTime = new Intent(SelectTimeActivity.this, AlterRecordActivity.class);
                    alterTime.putExtra("select_date", sdate);
                    alterTime.putExtra("select_time", stime);
                    Log.i(TAG, "onClick: select_time:" + stime);
                    setResult(RESULT_CODE_ALTER_TIME, alterTime);
                }
                finish();
            }
        });
    }

    //当日期中某位为10以内，将其显示为0x
    public String formatDateDouble(int i){
        String str = "";
        if(i < 10)
            str = "0" + i;
        else
            str = Integer.toString(i);
        return str;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        if(item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        super.onOptionsItemSelected(item);
        return true;
    }
}